//
//  NSDictionaryAdditions.m
//  MailDelivery
//
//  Created by Dante Palacios on 26/08/09.
//  Copyright 2009 Dante Palacios. All rights reserved.
//


#import "NSDictionaryAdditions.h"

#import <AppKit/NSImage.h>

#import "Funtions.h"
#import "Constants.h"

#import "TCPConnection.h"
#import "SMTPMailDelivery.h"

#import "NSDataAdditions.h"
#import "NSStringAdditions.h"
#import "NSArrayAdditions.h"


@implementation NSDictionary(NSDictionaryAdditions)

- (id)objectForCaseInsensitiveKey:(NSString *)aKey 
{
	id object = [self objectForKey:aKey];
	if (object) return object;
	
	for (NSString *key in self) 
	{
		if ([key isCaseInsensitiveEqualToString:aKey])
		{
			return [self objectForKey:aKey];
		}
	}
	
	return nil;
}

@end

@implementation NSDictionary(NSDictionaryMailDeliveryAdditions)

- (NSArray *)adresseesForMessageHeader:(NSString *)headerName {
	id object = [self objectForCaseInsensitiveKey:headerName];
	NSMutableArray *array = [NSMutableArray array];
	if ([object isKindOfClass:[NSString class]]) 
	{
		NSString *string = (NSString *)object;
		NSCharacterSet *splitSet = [NSCharacterSet characterSetWithCharactersInString:@";,"];
		if ((string != nil) && (![string isEqualToString:@""])) 
		{
			if ([string containsString:@";"] || [string containsString:@","]) 
			{
				NSArray *recipients = [[string stringByReplacingOccurrencesOfString:@" " withString:@""] componentsSeparatedByCharactersInSet:splitSet];
				[array addObjectsFromArray:[recipients arrayByExtractingAddresses]];
			}
			else {
				NSString *address = string.address;
				if (address) {
					[array addObject:address];
				}
			}
		}
	}
	else if ([object isKindOfClass:[NSArray class]]) 
	{
		[array addObjectsFromArray:[object arrayByExtractingAddresses]];
	}
	
	return array;
}

@end

@implementation NSDictionary (DeliveryAccountAdditions)

- (NSString *)name
{
	if ([self objectForKey:kDeliveryAccountName]) 
	{
		return [self objectForKey:kDeliveryAccountName];
	}
	return [[self emailAddresses] componentsJoinedByString:@", "];
}

- (NSString *)defaultSender 
{
	NSArray *emailAddresses = [self emailAddresses];
	if (!emailAddresses.count)
	{
		return nil;
	}
	
	NSString *fullName = [self fullName];
	NSString *address = [emailAddresses objectAtIndex:0];
	if (!address) 
	{
		return nil;
	}
	
	if (!fullName) 
	{
		fullName = @"";
	}
	
	NSMutableString *sender = [NSMutableString stringWithString:fullName];
	[sender appendFormat:!sender.length ? @"%@" : @" <%@>", address];
	
	return sender;
}

- (NSArray *)emailAddresses
{
	return [self objectForKey:kDeliveryAccountEmailAddresses];
}

- (NSString *)fullName
{
	return [self objectForKey:kDeliveryAccountFullUserName];
}

- (NSString *)hostName
{
	return [self objectForKey:kDeliveryAccountHostName];
}

- (NSString *)userName
{
	if ([self objectForKey:kDeliveryAccountUserName])
	{
		return [self objectForKey:kDeliveryAccountUserName];
	}
	
	NSArray *emailAddresses = [self emailAddresses];
	if (!emailAddresses.count) 
	{
		return nil;
	}
	
	return [[emailAddresses objectAtIndex:0] userName];
}

- (NSString *)password 
{
	return [self objectForKey:kDeliveryAccountPassword];
}

- (NSInteger)authentication 
{
	if ([self objectForKey:kDeliveryAccountAuthentication])
	{
		return [[self objectForKey:kDeliveryAccountAuthentication] integerValue];
	}
	return kAuthenticationPlain;
}

- (NSString *)identifier
{
	return [NSString stringWithFormat:@"%@:%@", self.hostName, self.userName];
}

- (NSImage *)icon 
{
	NSString *hostName = self.hostName;
	if (!hostName) 
	{
		NSArray *emailAddresses = [self emailAddresses];
		if (emailAddresses.count) 
		{
			hostName = [[emailAddresses objectAtIndex:0] hostName];
		}
	}
	
	return IsMobileMeHost(hostName) ? [NSImage imageNamed:NSImageNameDotMac] : [SMTPMailDelivery genericEmailAccountIconImage];
}

- (NSInteger)port
{
	if ([self objectForKey:kDeliveryAccountPort])
	{
		return [[self objectForKey:kDeliveryAccountPort] integerValue];
	}
	return 587;
}

- (BOOL)usesDefaultPorts
{
	if ([self objectForKey:kDeliveryAccountUsesDefaultPorts]) 
	{
		return [[self objectForKey:kDeliveryAccountUsesDefaultPorts] boolValue];
	}
	return YES;
}

- (BOOL)usesSsl
{
	if ([self objectForKey:kDeliveryAccountUsesSSL])
	{
		return [[self objectForKey:kDeliveryAccountUsesSSL] boolValue];
	}
	return YES;
}

- (NSInteger)SSLSecurityLevel
{
	if ([self objectForKey:kDeliveryAccountSSLSecurityLevel])
	{
		return [[self objectForKey:kDeliveryAccountSSLSecurityLevel] integerValue];
	}
	return [self usesSsl] ? MailDeliverySecurityLevelTLSv1 : MailDeliverySecurityLevelNone;
}

@end

@implementation NSMutableDictionary(NSMutableDictionaryAdditions)

- (void)removeObjectForCaseInsensitiveKey:(NSString *)aKey
{
	if ([self objectForKey:aKey])
	{
		[self removeObjectForKey:aKey];
		//return;
	}
	
	NSArray *allKeys = [self allKeys];
	for (NSString *key in allKeys)
	{
		if ([key isCaseInsensitiveEqualToString:aKey]) 
		{
			[self removeObjectForKey:key];
			break;
		}
	}
}

@end